home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / getopt.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  202 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """Parser for command line options.
  5.  
  6. This module helps scripts to parse the command line arguments in
  7. sys.argv.  It supports the same conventions as the Unix getopt()
  8. function (including the special meanings of arguments of the form `-'
  9. and `--').  Long options similar to those supported by GNU software
  10. may be used as well via an optional third argument.  This module
  11. provides two functions and an exception:
  12.  
  13. getopt() -- Parse command line options
  14. gnu_getopt() -- Like getopt(), but allow option and non-option arguments
  15. to be intermixed.
  16. GetoptError -- exception (class) raised with 'opt' attribute, which is the
  17. option involved with the exception.
  18. """
  19. __all__ = [
  20.     'GetoptError',
  21.     'error',
  22.     'getopt',
  23.     'gnu_getopt']
  24. import os
  25.  
  26. class GetoptError(Exception):
  27.     opt = ''
  28.     msg = ''
  29.     
  30.     def __init__(self, msg, opt = ''):
  31.         self.msg = msg
  32.         self.opt = opt
  33.         Exception.__init__(self, msg, opt)
  34.  
  35.     
  36.     def __str__(self):
  37.         return self.msg
  38.  
  39.  
  40. error = GetoptError
  41.  
  42. def getopt(args, shortopts, longopts = []):
  43.     '''getopt(args, options[, long_options]) -> opts, args
  44.  
  45.     Parses command line options and parameter list.  args is the
  46.     argument list to be parsed, without the leading reference to the
  47.     running program.  Typically, this means "sys.argv[1:]".  shortopts
  48.     is the string of option letters that the script wants to
  49.     recognize, with options that require an argument followed by a
  50.     colon (i.e., the same format that Unix getopt() uses).  If
  51.     specified, longopts is a list of strings with the names of the
  52.     long options which should be supported.  The leading \'--\'
  53.     characters should not be included in the option name.  Options
  54.     which require an argument should be followed by an equal sign
  55.     (\'=\').
  56.  
  57.     The return value consists of two elements: the first is a list of
  58.     (option, value) pairs; the second is the list of program arguments
  59.     left after the option list was stripped (this is a trailing slice
  60.     of the first argument).  Each option-and-value pair returned has
  61.     the option as its first element, prefixed with a hyphen (e.g.,
  62.     \'-x\'), and the option argument as its second element, or an empty
  63.     string if the option has no argument.  The options occur in the
  64.     list in the same order in which they were found, thus allowing
  65.     multiple occurrences.  Long and short options may be mixed.
  66.  
  67.     '''
  68.     opts = []
  69.     if type(longopts) == type(''):
  70.         longopts = [
  71.             longopts]
  72.     else:
  73.         longopts = list(longopts)
  74.     while args and args[0].startswith('-') and args[0] != '-':
  75.         if args[0] == '--':
  76.             args = args[1:]
  77.             break
  78.         if args[0].startswith('--'):
  79.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  80.             continue
  81.         (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  82.     return (opts, args)
  83.  
  84.  
  85. def gnu_getopt(args, shortopts, longopts = []):
  86.     """getopt(args, options[, long_options]) -> opts, args
  87.  
  88.     This function works like getopt(), except that GNU style scanning
  89.     mode is used by default. This means that option and non-option
  90.     arguments may be intermixed. The getopt() function stops
  91.     processing options as soon as a non-option argument is
  92.     encountered.
  93.  
  94.     If the first character of the option string is `+', or if the
  95.     environment variable POSIXLY_CORRECT is set, then option
  96.     processing stops as soon as a non-option argument is encountered.
  97.  
  98.     """
  99.     opts = []
  100.     prog_args = []
  101.     if isinstance(longopts, str):
  102.         longopts = [
  103.             longopts]
  104.     else:
  105.         longopts = list(longopts)
  106.     if shortopts.startswith('+'):
  107.         shortopts = shortopts[1:]
  108.         all_options_first = True
  109.     elif os.environ.get('POSIXLY_CORRECT'):
  110.         all_options_first = True
  111.     else:
  112.         all_options_first = False
  113.     while args:
  114.         if args[0] == '--':
  115.             prog_args += args[1:]
  116.             break
  117.         if args[0][:2] == '--':
  118.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  119.             continue
  120.         if args[0][:1] == '-' and args[0] != '-':
  121.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  122.             continue
  123.         if all_options_first:
  124.             prog_args += args
  125.             break
  126.             continue
  127.         prog_args.append(args[0])
  128.         args = args[1:]
  129.     return (opts, prog_args)
  130.  
  131.  
  132. def do_longs(opts, opt, longopts, args):
  133.     
  134.     try:
  135.         i = opt.index('=')
  136.     except ValueError:
  137.         optarg = None
  138.  
  139.     opt = opt[:i]
  140.     optarg = opt[i + 1:]
  141.     (has_arg, opt) = long_has_args(opt, longopts)
  142.     if has_arg or optarg is None:
  143.         if not args:
  144.             raise GetoptError('option --%s requires argument' % opt, opt)
  145.         optarg = args[0]
  146.         args = args[1:]
  147.     
  148.     None((opts.append, '--' + opt if optarg is not None else ''))
  149.     return (opts, args)
  150.  
  151.  
  152. def long_has_args(opt, longopts):
  153.     possibilities = [ o for o in longopts if o.startswith(opt) ]
  154.     if not possibilities:
  155.         raise GetoptError('option --%s not recognized' % opt, opt)
  156.     if opt in possibilities:
  157.         return (False, opt)
  158.     if None + '=' in possibilities:
  159.         return (True, opt)
  160.     if None(possibilities) > 1:
  161.         raise GetoptError('option --%s not a unique prefix' % opt, opt)
  162.     if not len(possibilities) == 1:
  163.         raise AssertionError
  164.     unique_match = None[0]
  165.     has_arg = unique_match.endswith('=')
  166.     if has_arg:
  167.         unique_match = unique_match[:-1]
  168.     return (has_arg, unique_match)
  169.  
  170.  
  171. def do_shorts(opts, optstring, shortopts, args):
  172.     while optstring != '':
  173.         opt = optstring[0]
  174.         optstring = optstring[1:]
  175.         if short_has_arg(opt, shortopts):
  176.             if optstring == '':
  177.                 if not args:
  178.                     raise GetoptError('option -%s requires argument' % opt, opt)
  179.                 optstring = args[0]
  180.                 args = args[1:]
  181.             optarg = optstring
  182.             optstring = ''
  183.         else:
  184.             optarg = ''
  185.         opts.append(('-' + opt, optarg))
  186.     return (opts, args)
  187.  
  188.  
  189. def short_has_arg(opt, shortopts):
  190.     for i in range(len(shortopts)):
  191.         if shortopts[i] == shortopts[i]:
  192.             pass
  193.         elif shortopts[i] != ':':
  194.             return shortopts.startswith(':', i + 1)
  195.     raise GetoptError('option -%s not recognized' % opt, opt)
  196.  
  197. if __name__ == '__main__':
  198.     import sys
  199.     print getopt(sys.argv[1:], 'a:b', [
  200.         'alpha=',
  201.         'beta'])
  202.